<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\currency\service;

use GuzzleHttp\Client;
use InvalidArgumentException;
use WeChat\Contracts\DataArray;
use think\Db;

/**
 * 短信业务扩展服务
 * Class ExtendService
 * @package app\currency\service
 */
class ExtendService
{
    const SMS_SERVICE_URI = 'http://api.qirui.com:7891';
    /**
     * 静态配置
     * @var DataArray
     */
    protected $config;

    /**
     * ExtendService constructor.
     * @param array $options
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function __construct(array $options = [])
    {
        if (empty($options)) $options = [
            'un'      => sysconf('sms_xs_username'),
            'pw'      => sysconf('sms_xs_password'),
        ];
        if (empty($options['un'])) {
            throw new InvalidArgumentException("Missing Config -- [api_key]");
        }
        if (empty($options['pw'])) {
            throw new InvalidArgumentException("Missing Config -- [api_secret]");
        }
        $this->config = new DataArray($options);
    }

    /**
     * 发送短信验证码
     * @param string $phone 手机号
     * @param string $content 短信内容
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function sendSms($phone, $content)
    {
        $uri = self::SMS_SERVICE_URI . "/mt?dc=8&un={$this->config->get('un')}&pw={$this->config->get('pw')}&sm={$content}&da={$phone}&tf=3&rf=2&rd=0";
        $client = new Client();
        $res = $client->request('GET', $uri);
        $result = $res->getBody()->getContents();
        $result = json_decode($result,true);
        if(isset($result['success']) && $result['success'] === true){
            $insert = ['phone' => $phone, 'content' => $content, 'result' => json_encode($result)];
            Db::name('MemberSmsHistory')->insert($insert);
            return true;
        }
        return false;
    }

    /**
     * 查询短信余额
     * @return array|bool|mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function querySmsBalance()
    {
        $uri = self::SMS_SERVICE_URI . "/bi?un={$this->config->get('un')}&pw={$this->config->get('pw')}&rf=2";
        $client = new Client();
        $res = $client->request('GET', $uri);
        $result = $res->getBody()->getContents();
        $result = json_decode($result,true);
        if(isset($result['success']) && $result['success'] === true){
            return ['code' => 1, 'num' => isset($result['bl']) ? $result['bl'] : 0, 'msg' => '获取短信剩余条数成功！'];
        } else {
            return ['code' => 0, 'num' => '0', 'msg' => '获取短信剩余条数失败！'];
        }
    }

}
